package com.stgy.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stgy.lease.common.exception.StgyException;
import com.stgy.lease.common.result.ResultCodeEnum;
import com.stgy.lease.model.domain.FeeKey;
import com.stgy.lease.model.domain.FeeValue;
import com.stgy.lease.web.admin.service.FeeKeyService;
import com.stgy.lease.web.admin.mapper.FeeKeyMapper;
import com.stgy.lease.web.admin.service.FeeValueService;
import com.stgy.lease.web.admin.vo.FeeKeyVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lingx
 * @description 针对表【fee_key(杂项费用名称表)】的数据库操作Service实现
 * @createDate 2024-08-28 11:00:29
 */
@Service
public class FeeKeyServiceImpl extends ServiceImpl<FeeKeyMapper, FeeKey>
        implements FeeKeyService {

    @Autowired
    private FeeValueService feeValueService;

    //添加或修改公寓杂费列表和值
    @Override
    public boolean saveOrUpdateFeeValue(FeeValue feeValue) {
        if (feeValue == null) {
            throw new StgyException(ResultCodeEnum.DATA_ERROR);
        }
        LambdaQueryWrapper<FeeValue> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FeeValue::getName, feeValue.getName());
        wrapper.eq(FeeValue::getFeeKeyId, feeValue.getFeeKeyId());
        wrapper.eq(FeeValue::getUnit, feeValue.getUnit());
        wrapper.last("limit 1");
        FeeValue one = feeValueService.getOne(wrapper);
        if (one != null && !one.getId().equals(feeValue.getId())) {
            throw new StgyException(ResultCodeEnum.DATA_ERROR);
        }
        return feeValueService.saveOrUpdate(feeValue);
    }

    //添加或修改属性
    @Override
    public boolean saveOrUpdateFeeKeyVo(FeeKeyVo feeKeyVo) {
        if (feeKeyVo == null) {
            throw new StgyException(ResultCodeEnum.DATA_ERROR);
        }
        LambdaQueryWrapper<FeeKey> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FeeKey::getName, feeKeyVo.getName());
        wrapper.last("limit 1");
        FeeKey one = this.getOne(wrapper);
        if (one != null && !one.getId().equals(feeKeyVo.getId())) {
            throw new StgyException(ResultCodeEnum.DATA_ERROR);
        }
        return this.saveOrUpdate(feeKeyVo);
    }

    ////根据id删除杂物列表
    @Override
    public boolean delectByfeeKeyId(Long id) {
        if (this.getById(id) == null || id == null) {
            throw new StgyException(ResultCodeEnum.DATA_ERROR);
        }
        LambdaQueryWrapper<FeeValue> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FeeValue::getFeeKeyId, id);
        feeValueService.remove(wrapper);
        return this.removeById(id);
    }

    /*
    查询feekey查询了所有的未删除的key：fee_key
    查询feeValue 最终组合起来也是fee_value表 未删除数据
    只需要查询2次，在业务代码中组装数据即可
     */

    @Override
    public List<FeeKeyVo> selectFeeKeyVos() {
        //表数据量不大 两张表连接字段没有索引时，推荐
        List<FeeKey> feeKeys = this.list();
        List<FeeValue> feeValues = feeValueService.list();

        return feeKeys.stream().map(feeKey -> {
            //创建新的vo对象
            FeeKeyVo feeKeyVo = new FeeKeyVo();
            //将feekey属性值复制刚给vo对象
            BeanUtils.copyProperties(feeKey, feeKeyVo);
            //从feevalue集合中挑出feekeyvo的值 feevale的feeKeyId == feeKey的id
            List<FeeValue> feeValueList = feeValues.stream()
                    .filter(feeValue -> feeValue.getFeeKeyId().equals(feeKey.getId()))
                    .collect(Collectors.toList());
            feeKeyVo.setFeeValueList(feeValueList);

            return feeKeyVo;
        }).collect(Collectors.toList());
    }

//    @Override
//    public List<FeeKeyVo> selectFeeKeyVos() {
//        List<FeeKey> feeKeys = this.list();
//        //遍历集合 将每个对象转为指定类型的对象
//        return feeKeys.stream().map(feeKey -> {
//            FeeKeyVo feeKeyVo = new FeeKeyVo();
//            //BeanUtils 可以将数据源对象的相同类型 相同名称的属性值设置给目标对象
//            BeanUtils.copyProperties(feeKey, feeKeyVo);
//            //需要给feeKeyVo查询feeValueList集合
//            LambdaQueryWrapper<FeeValue> wrapper = new LambdaQueryWrapper<>();
//
//            wrapper.eq(FeeValue::getFeeKeyId, feeKey.getId());
//            feeKeyVo.setFeeValueList(feeValueService.list(wrapper));
//            //返回转换后的vo对象
//            return feeKeyVo;
//        }).collect(Collectors.toList());
//    }
}